package com.base.taskreports.task.job;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;

import com.base.taskreports.common.utlis.DateUtil;
import com.base.taskreports.common.utlis.ExcelUtils;
import com.base.taskreports.common.utlis.FileUtils;
import com.base.taskreports.demoExel.entity.Indicator;
import com.base.taskreports.reports.entiyt.master.ExcelBean;
import com.base.taskreports.reports.service.TIndicatorsService;
import com.base.taskreports.task.service.JfsService;
import com.base.taskreports.task.service.JfuService;
import com.vip.saturn.job.AbstractSaturnJavaJob;
import com.vip.saturn.job.SaturnJobExecutionContext;
import com.vip.saturn.job.SaturnJobReturn;

public class ExeclJob extends AbstractSaturnJavaJob {

    @Autowired
    private TIndicatorsService tIndicatorsService;


    @Autowired
    private JfuService jfuService;
    @Autowired
    private JfsService jfsService;

    @Value("${excelUrl}")
    private String excelUrl;

    @Override
    public SaturnJobReturn handleJavaJob(String jobName, Integer shardItem, String shardParam,
                                         SaturnJobExecutionContext shardingContext) throws InterruptedException {

        String DateTime = shardingContext.getJobParameter();

        String toDay = null;
        long startTime = System.currentTimeMillis();

        try {

            if (!StringUtils.isEmpty(DateTime)) {
                Date string2Date = DateUtil.string2Date(DateTime);
                toDay = DateUtil.format(string2Date, "yyyyMMdd");
            } else {
                toDay = DateUtil.format(new Date(), "yyyyMMdd");
            }

            //String excelUrl = "D:\\\\Users\\\\admin\\\\git\\\\task-reports1\\\\src\\\\main\\\\java\\\\com\\\\base\\\\taskreports\\\\common\\\\exel\\\\";
            String outPath = excelUrl + "报表展示模板.xlsx";
            String newPath = excelUrl + "报表展示模板_" + toDay + ".xlsx";

            int stratRow = 3;


            FileUtils.deleteFile(newPath);
            FileUtils.copyFileUsingJava7Files(new File(outPath), new File(newPath));

            ExcelUtils.addHeaderDate(newPath); // 为所有 Sheet 表头加入日期


            List<Object> objectList = null;

            // for (Indicator indicator : Indicator.values()) {
            //list = new ArrayList();
            //非货币积分发放金额浮动比率
            List<ExcelBean> getJFS_M001 = tIndicatorsService.getJFS_M001(DateTime);

            this.selectSevenDaysAgo(getJFS_M001, objectList, newPath, stratRow, Indicator.JFS_M001.getIndex(), startTime);


            // 非人民币货币积分发放金额浮动比率
            List<ExcelBean> getJFS_M002 = tIndicatorsService.getJFS_M002(DateTime);
            this.selectSevenDaysAgo(getJFS_M002, objectList, newPath, stratRow, Indicator.JFS_M002.getIndex(), startTime);

            // 事后结算积分发放金额浮动比率
            List<ExcelBean> getJFS_M003 = tIndicatorsService.getJFS_M003(DateTime);
            this.selectSevenDaysAgo(getJFS_M003, objectList, newPath, stratRow, Indicator.JFS_M003.getIndex(), startTime);

            // 单用户货币积分余额> N浮动比率
            List<ExcelBean> getJFS_M004 = tIndicatorsService.getJFS_M004(DateTime);
            this.selectThreeDaysAgo(getJFS_M004, objectList, newPath, stratRow, Indicator.JFS_M004.getIndex(), startTime);

            //单用户货币积分余额> 	前50条写入报表
            List<Object> getJFS_M004_50 = jfsService.getJFS_M004_Detail(DateTime);
            this.selectFiftyDaysAgo(getJFS_M004_50, objectList, newPath, 10, Indicator.JFS_M004.getIndex(), startTime);

            // 单用户非货币积分余额> N浮动比率
            List<ExcelBean> getJFS_M005 = tIndicatorsService.getJFS_M005(DateTime);
            this.selectThreeDaysAgo(getJFS_M005, objectList, newPath, stratRow, Indicator.JFS_M005.getIndex(), startTime);

            // 单用户非货币积分余额> N浮动比率	前50条写入报表
            List<Object> getJFS_M005_50 = jfsService.getJFS_M005_Detail(DateTime);
            this.selectFiftyDaysAgo(getJFS_M005_50, objectList, newPath, 10, Indicator.JFS_M005.getIndex(), startTime);

            // 临时用户（非注册用户,即不使用userID用户）交易数量浮动比										
            List<ExcelBean> getJFS_M006 = tIndicatorsService.getJFS_M006(DateTime);
            this.selectSevenDaysAgo(getJFS_M006, objectList, newPath, stratRow, Indicator.JFS_M006.getIndex(), startTime);

            // 积分余额为负数量				
            List<ExcelBean> getJFS_M007 = tIndicatorsService.getJFS_M007(DateTime);
            this.selectThreeDaysAgo(getJFS_M007, objectList, newPath, stratRow, Indicator.JFS_M007.getIndex(), startTime);

            // 积分余额为负数量	前50条写入报表
            List<Object> getJFS_M007_50 = jfsService.getJFS_M007_Detail(DateTime);
            this.selectFiftyDaysAgo(getJFS_M007_50, objectList, newPath, 10, Indicator.JFS_M007.getIndex(), startTime);

            //外部积分（转接/兑换）承兑金额浮动告警
            List<ExcelBean> getJFU_M001 = tIndicatorsService.getJFU_M001(DateTime);
            this.selectSevenDaysAgo(getJFU_M001, objectList, newPath, stratRow, Indicator.JFU_M001.getIndex(), startTime);

            //各积分类别消费中各支付产品类型笔数占比浮动
            List<ExcelBean> getJFU_M002 = tIndicatorsService.getJFU_M002(DateTime);
            this.selectSevenDaysAgo(getJFU_M002, objectList, newPath, stratRow, Indicator.JFU_M002.getIndex(), startTime);

            //各积分类别消费中各支付产品类型金额占比浮动
            List<ExcelBean> getJFU_M003 = tIndicatorsService.getJFU_M003(DateTime);
            this.selectSevenDaysAgo(getJFU_M003, objectList, newPath, stratRow, Indicator.JFU_M003.getIndex(), startTime);

            //积分撤销/冲正/退货反向交易笔数占比浮动
            List<ExcelBean> getJFU_M004 = tIndicatorsService.getJFU_M004(DateTime);
            this.selectSevenDaysAgo(getJFU_M004, objectList, newPath, stratRow, Indicator.JFU_M004.getIndex(), startTime);

            //积分撤销/冲正/退货反向交易金额占比浮动
            List<ExcelBean> getJFU_M005 = tIndicatorsService.getJFU_M005(DateTime);
            this.selectSevenDaysAgo(getJFU_M005, objectList, newPath, stratRow, Indicator.JFU_M005.getIndex(), startTime);

            //营销系统系统成功率
            List<ExcelBean> getJFU_M006 = tIndicatorsService.getJFU_M006(DateTime);
            this.selectSevenDaysAgo(getJFU_M006, objectList, newPath, stratRow, Indicator.JFU_M006.getIndex(), startTime);

            //积分兑换价值比不为1笔数
            List<ExcelBean> getJFU_M007 = tIndicatorsService.getJFU_M007(DateTime);
            this.selectThreeDaysAgo(getJFU_M007, objectList, newPath, stratRow, Indicator.JFU_M007.getIndex(), startTime);

            //积分兑换价值比不为1笔数 前50
            List<Object> getJFU_M007_50 = jfuService.getJFU_M007_Detail(DateTime);
            this.selectFiftyDaysAgo(getJFU_M007_50, objectList, newPath, 10, Indicator.JFU_M007.getIndex(), startTime);


            //已失效积分金额浮动比率
            List<ExcelBean> getJFU_M008 = tIndicatorsService.getJFU_M008(DateTime);
            this.selectSevenDaysAgo(getJFU_M008, objectList, newPath, stratRow, Indicator.JFU_M008.getIndex(), startTime);



        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //System.out.println(ExcelUtils.addExcelSheetContent(newPath, indicator.getIndex(), stratRow, list) ? "成功" : "失败");
        // }
        catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("-------------------------" + (System.currentTimeMillis() - startTime));


        return new SaturnJobReturn("I am the return result of shard item " + shardItem);
    }


    //积分 --查询前三天的
    public void selectThreeDaysAgo(List<ExcelBean> excelBeanList, List<Object> objectList, String newPath, int stratRow, int index, long startTime) throws IOException {
        List<List<Object>> list = new ArrayList<List<Object>>();
        for (ExcelBean excelBean : excelBeanList) {
            objectList = new ArrayList<Object>();
            objectList.add(excelBean.getT3());
            objectList.add(excelBean.getT2());
            objectList.add(excelBean.getT1());
            objectList.add(excelBean.getThresholdValue());
            objectList.add(excelBean.getIsOverLimit());
            list.add(objectList);
        }
        System.out.println(ExcelUtils.addExcelSheetContent(newPath, index, stratRow, list) ? "成功" : "失败");
        System.out.print(System.currentTimeMillis() - startTime);

    }

    //积分 --查询前50天的
    public void selectFiftyDaysAgo(List<Object> excelBeanList, List<Object> objectList, String newPath, int stratRow, int index, long startTime) throws IOException {
        List<List<Object>> list = new ArrayList<List<Object>>();
        list.add(excelBeanList);
        System.out.println(ExcelUtils.addExcelSheetContent(newPath, index, stratRow, list) ? "成功" : "失败");
        System.out.print(System.currentTimeMillis() - startTime);

    }


    //积分 --查询前8天的
    public void selectSevenDaysAgo(List<ExcelBean> excelBeanList, List<Object> objectList, String newPath, int stratRow, int index, long startTime) throws IOException {
        List<List<Object>> list = new ArrayList<List<Object>>();
        for (ExcelBean excelBean : excelBeanList) {
            objectList = new ArrayList<Object>();
            objectList.add(excelBean.getT8());
            objectList.add(excelBean.getT7());
            objectList.add(excelBean.getT6());
            objectList.add(excelBean.getT5());
            objectList.add(excelBean.getT4());
            objectList.add(excelBean.getT3());
            objectList.add(excelBean.getT2());
            objectList.add(excelBean.getT1());
            objectList.add(excelBean.getFalotRate());
            objectList.add(excelBean.getThresholdValue());
            objectList.add(excelBean.getIsOverLimit());
            list.add(objectList);
        }

        System.out.println(ExcelUtils.addExcelSheetContent(newPath, index, stratRow, list) ? "成功" : "失败");
        System.out.print(System.currentTimeMillis() - startTime);

    }
}
